<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="searchInpt"
        placeholder="输入ID"
        clearable
        @keyup.enter.native="search()"
        class="search-btn"
      >
        <el-button
          type="primary"
          @click="search()"
          icon="el-icon-search"
          slot="append"
          :loading="searchLoading"
        >搜索</el-button>
      </el-input>
    </div>
    <div class="table-container">
      <Table :table="dataTable">
      </Table>
      <div
        class="Pagination"
        style="text-align: left;margin-top: 10px;"
      >
        <el-pagination
          v-if="pageshow"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { fetchAnalyzeLog } from "@/api/table";
import format from "date-format";
import Table from "@/components/Table";
export default {
  data() {
    return {
      //总页码
      count: 0,
      //单页行数
      limit: 20,
      //页码
      currentPage: 1,
      searchInpt: "",
      searchLoading: false,
      pageshow: true,
      dataTable: {
        loading: false,
        border: true,
        sortChange: "",
        hasOperation: false,
        tr: [
          {
            id: "1",
            label: "id",
            prop: "event",
            show: true
          },
          {
            id: "2",
            label: "时间",
            prop: "clickTime",
            show: true
          },
          {
            id: "3",
            label: "页面/按钮",
            prop: "eventName",
            show: true
          }
        ],
        data: [],
        operation: {}
      }
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getAnalyzeLog();
    },
    search() {
      this.currentPage = 1;
      this.pageshow = false;
      this.getAnalyzeLog();
      this.$nextTick(() => {
        this.pageshow = true;
      });
    },
    //时间格式化
    formatterTime(t) {
      if (t) {
        const newTime = format("yyyy-MM-dd hh:mm:ss", new Date(t)).split(" ");
        let time = "";
        time += newTime[0] + "\n" + newTime[1];
        return time;
      } else {
        return "";
      }
    },
    async getAnalyzeLog() {
      this.dataTable.loading = true;
      this.searchLoading = true;
      const res = await fetchAnalyzeLog({
        pageSize: this.limit,
        pageNo: this.currentPage,
        userId: this.searchInpt
      });
      this.searchLoading = false;
      this.dataTable.loading = false;
      this.dataTable.data = [];
      this.count = res.data.total;
      res.data.list.forEach(item => {
        const data = {};
        data.eventName = item.eventName;
        data.clickTime = this.formatterTime(item.clickTime);
        data.event = item.event;
        this.dataTable.data.push(data);
      });
    }
  },
  components: {
    Table
  }
};
</script>
